package cn.kn.triage.zdx.manualmapper;

import cn.kn.triage.zdx.manualmodel.Dept;
import cn.kn.triage.zdx.manualmodel.Doctor;
import cn.kn.triage.zdx.view.Shiftview;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Service;

import java.util.List;

//When time's running short, you gotta improvise
//Yes I'm writing an ALL-IN-ONE mapper again
@Mapper
@Service
public interface AllinOne {
//    todo:不要忘记手动添加isdel属性
    @Select("set @mon = (select date_add(#{param1}, interval 0 day));\n" +
            "set @tue = (select date_add(#{param1}, interval 1 day));\n" +
            "set @wen = (select date_add(#{param1}, interval 2 day));\n" +
            "set @thu = (select date_add(#{param1}, interval 3 day));\n" +
            "set @fri = (select date_add(#{param1}, interval 4 day));\n" +
            "set @sat = (select date_add(#{param1}, interval 5 day));\n" +
            "set @sun = cast(#{param2} as date);\n" +
            "set @deptid = cast(#{param3} as unsigned);\n" +
            "prepare stmt from \"select s.docid, doctor.dname, s.sdate, \n" +
            "    case when sdate = ? then shift end as mon,\n" +
            "    case when sdate = ? then shift end as tue,\n" +
            "    case when sdate = ? then shift end as wen,\n" +
            "    case when sdate = ? then shift end as thu,\n" +
            "    case when sdate = ? then shift end as fri,\n" +
            "    case when sdate = ? then shift end as sat,\n" +
            "    case when sdate = ? then shift end as sun\n" +
            "  from doc_scheduling as s\tINNER JOIN\n" +
            "\tdoctor\n" +
            "\tON \n" +
            "\t\ts.docid = doctor.did\n" +
            "WHERE\n" +
            "\tsdate BETWEEN ? AND ? \n" +
            "\tAND doctor.ddid = ?\n" +
            "ORDER BY\n" +
            "\ts.sdate ASC, \n" +
            "\tdoctor.dname ASC\";\n" +
            "\n" +
            "execute stmt using @mon,@tue,@wen,@thu,@fri,@sat,@sun,@mon,@sun,@deptid;")
    public List<Shiftview> extractShift(String startDate, String endDate, String deptid);

    @Select("SELECT\n" +
            "\tdoctor.did, \n" +
            "\tdoctor.dname\n" +
            "FROM\n" +
            "\tdoctor\n" +
            "WHERE\n" +
            "\tdoctor.ddid = 1 and doctor.isdel = 0")
    public List<Doctor> doctorlist(String deptid);

    @Select("select id, `name` from tri_dept")
    public List<Dept> deptlist();
}
